<template>
	<view class="page">
		<doubleList :leftList="leftList" :rightList="rightList" @leftItemClick="leftItemClick"
			@rightItemClick="productItemClicked">
		</doubleList>
	</view>
</template>

<script>
	import doubleList from '../../components/doubleList/index.vue'
	import {
	  mapState,
	  mapMutations,
	  mapGetters
	 } from 'vuex';
	export default {
		components: {
			doubleList
		},
		data() {
			return {
				currentCategoryId: "",
				page: 1,
				size: 10,
				leftList: [{
						name: '全部',
						id: 0
					},
					{
						name: '紧凑型',
						id: 1
					},
					{
						name: 'MPV',
						id: 2
					},
					{
						name: '商务型',
						id: 3
					},
					{
						name: '豪华型',
						id: 4
					},
				],

				rightList: [
					// 				{
					// 				img: require('@/static/img/carDetail/car.png'),
					// 				name: '大众朗逸',
					// 				id: 'C_BN63PmZSde7',
					// 				price: 6000,
					// 				final_price: 3000,

					// 			},{
					// 				img: require('@/static/img/carDetail/car.png'),
					// 				name: '大众朗逸',
					// 				id: 'C_BN63PmZSde7',
					// 				price: 6000,
					// 				final_price: 3000,

					// 			},
				]
			};
		},
		onLoad() {

			//获取类型
			this.getCategory();

		},

		onReachBottom() {
			this.getProductList()
		},


		methods: {

			init() {
				this.page = 1;
				this.size = 10;
				this.rightList = [];
			},

			leftItemClick(e) {
				this.init();
				console.log("-----categoryId:" + e.categoryId)
				this.currentCategoryId = e.categoryId
				this.getProductList();

			},
			productItemClicked(e) {
				console.log("e:---" + e.productId,e.name,e.imgSrc);
				this.$store.commit("setProductId",e.productId);
				this.$store.commit("setProductName",e.name);
				this.$store.commit("setImgSrc",e.imgSrc);
				// this.$store.commit("setImgSrc",e.imgSrc);
				// this.$store.commit("setImgSrc",e.imgSrc);
				
				uni.navigateBack({
					
				})

			},
			getCategory() {
				let that = this
				this.$pin.request('GET', '/categories', null,
					successData => {
						if (successData.code == that.$pin.code.success) {
							var categoryList = successData.data;
							if (categoryList.length > 0) {
								that.currentCategoryId = categoryList[0].id;
								that.getProductList()
							}
							that.leftList = categoryList;

						}
					},
					failData => {
						uni.showToast({
							icon: 'none',
							title: '获取分类信息失败'
						})
					})
			},
			getProductList() {
				let that = this

				var param = {
					categories_id: this.currentCategoryId ? this.currentCategoryId : "",
					page: that.page,
					size: that.size
				}
				this.$pin.request('GET', '/products', param,
					successData => {
						uni.stopPullDownRefresh();
						if (successData.code == that.$pin.code.success) {
							var records = successData.data.records;
							if (records.length > 0) {
								that.rightList = that.rightList.concat(records);
								this.page++;
							} else {
								uni.showToast({
									title: '没有更多数据',
									icon: 'none'
								})
							}

						}
					},
					failData => {
						uni.showToast({
							icon: 'none',
							title: '获取分类信息失败'
						})
					})
			}
		}
	}
</script>

<style lang="scss">

</style>